package com.lavida.project.system.goods.service;

import java.util.List;

import org.apache.poi.ddf.EscherSerializationListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lavida.project.system.goods.domain.Goods;
import com.lavida.project.system.goods.domain.SubGoods;
import com.lavida.project.system.goods.mapper.GoodsMapper;
import com.lavida.project.system.goods.mapper.SubGoodsMapper;

@Service
public class SubGoodsServiceImpl implements SubGoodsService {
	@Autowired
	private SubGoodsMapper subGoodsMapper;

	@Autowired
	private GoodsMapper goodsMapper;

	@Override
	public List<SubGoods> selectById(int id) {
		// TODO Auto-generated method stub
		return subGoodsMapper.selectById(id);
	}

	@Override
	public int insertSubGoods(SubGoods subGoods) {
		Goods goods = goodsMapper.getGoods(subGoods.getSubId());
		// TODO Auto-generated method stub
		if (subGoods.getOperateType().equals("报废") || subGoods.getOperateType().equals("领用")) {
			goods.setGoods_num(goods.getGoods_num() - subGoods.getAmount());
		} else {
			goods.setGoods_num(goods.getGoods_num() + subGoods.getAmount());
		}
		goodsMapper.updateGoods(goods);
		return subGoodsMapper.insertSubGoods(subGoods);
	}

	@Override
	public int deleteSubgoods(int id) {
		SubGoods subGoods = subGoodsMapper.getSubgoods(id);
		Goods goods = goodsMapper.getGoods(subGoods.getSubId());
		// TODO Auto-generated method stub
		if (subGoods.getOperateType().equals("报废")) {
			goods.setGoods_num(goods.getGoods_num() + subGoods.getAmount());
		} else if(subGoods.getOperateType().equals("领用")){
			goods.setGoods_num(goods.getGoods_num() + subGoods.getAmount());
		}else {
			goods.setGoods_num(goods.getGoods_num() - subGoods.getAmount());
		}
		goodsMapper.updateGoods(goods);
		return subGoodsMapper.deleteSubgoods(id);
	}

	@Override
	public int updateSubgoods(SubGoods subGoods) {
		SubGoods subGoods1 = subGoodsMapper.getSubgoods(subGoods.getId());
		System.out.println("subGoods1:"+subGoods1.toString());
		System.out.println("subGoods1:" + subGoods1.getOperateType() + ">>>>>>" + "subGoods:" + subGoods.getOperateType());
		Goods goods = goodsMapper.getGoods(subGoods.getSubId());
			if (subGoods1.getOperateType().equals("领用") && subGoods.getOperateType().equals("领用")) {
				if (subGoods1.getAmount() == subGoods.getAmount()) {
					System.out.println("未修改");
				} else if (subGoods1.getAmount() > subGoods.getAmount()) {
					int amount = subGoods1.getAmount() - subGoods.getAmount();
					goods.setGoods_num(goods.getGoods_num() - amount);
				} else if (subGoods1.getAmount() < subGoods.getAmount()) {
					int amount = subGoods.getAmount() - subGoods1.getAmount();
					goods.setGoods_num(goods.getGoods_num() + amount);
				}
			}else if(subGoods1.getOperateType().equals("报废")&&subGoods.getOperateType().equals("报废")) {
				if (subGoods1.getAmount() == subGoods.getAmount()) {
					System.out.println("未修改");
				} else if (subGoods1.getAmount() > subGoods.getAmount()) {
					int amount = subGoods1.getAmount() - subGoods.getAmount();
					goods.setGoods_num(goods.getGoods_num() - amount);
				} else if(subGoods1.getAmount() < subGoods.getAmount()){
					int amount = subGoods.getAmount() - subGoods1.getAmount();
					goods.setGoods_num(goods.getGoods_num() + amount);
				}
			}else if(subGoods1.getOperateType().equals("归还") && subGoods.getOperateType().equals("归还")) {
					if (subGoods1.getAmount() == subGoods.getAmount()) {
						System.out.println("未修改");
					} else if (subGoods1.getAmount() > subGoods.getAmount()) {
						int amount = subGoods1.getAmount() - subGoods.getAmount();
						goods.setGoods_num(goods.getGoods_num() - amount);
					} else if(subGoods1.getAmount() < subGoods.getAmount()){
						int amount = subGoods1.getAmount() - subGoods.getAmount();
						goods.setGoods_num(goods.getGoods_num() + amount);
					}

			}else if (subGoods1.getOperateType().equals("报废") && subGoods.getOperateType().equals("领用")) {
				if (subGoods1.getAmount() == subGoods.getAmount()) {
					System.out.println("未修改");
				} else if (subGoods1.getAmount() > subGoods.getAmount()) {
					int amount = subGoods1.getAmount() - subGoods.getAmount();
					goods.setGoods_num(goods.getGoods_num() - amount);
				} else if (subGoods1.getAmount() < subGoods.getAmount()) {
					int amount = subGoods.getAmount() - subGoods1.getAmount();
					goods.setGoods_num(goods.getGoods_num() + amount);
				}
			} else if (subGoods1.getOperateType().equals("报废") && subGoods.getOperateType().equals("归还")) {
				if (subGoods1.getAmount() == subGoods.getAmount()) {
					int amount = subGoods.getAmount() + subGoods1.getAmount();
					goods.setGoods_num(goods.getGoods_num() + amount);
				} else if (subGoods1.getAmount() > subGoods.getAmount()) {
					int amount = subGoods.getAmount() + subGoods1.getAmount();
					goods.setGoods_num(goods.getGoods_num() + amount);
				} else if (subGoods1.getAmount() < subGoods.getAmount()) {
					int amount = subGoods.getAmount() + subGoods1.getAmount();
					goods.setGoods_num(goods.getGoods_num() + amount);
				}
			} else if(subGoods1.getOperateType().equals("领用") && subGoods.getOperateType().equals("归还")) {
				if (subGoods1.getAmount() == subGoods.getAmount()) {
					int amount = subGoods.getAmount() + subGoods1.getAmount();
					goods.setGoods_num(goods.getGoods_num() + amount);
				} else if (subGoods1.getAmount() > subGoods.getAmount()) {
					int amount = subGoods.getAmount() + subGoods1.getAmount();
					goods.setGoods_num(goods.getGoods_num() + amount);
				} else if (subGoods1.getAmount() < subGoods.getAmount()) {
					int amount = subGoods.getAmount() + subGoods1.getAmount();
					goods.setGoods_num(goods.getGoods_num() + amount);
				}
				
			} else if(subGoods1.getOperateType().equals("领用") && subGoods.getOperateType().equals("报废")) {
				if (subGoods1.getAmount() == subGoods.getAmount()) {
					System.out.println("未修改");
				} else if (subGoods1.getAmount() > subGoods.getAmount()) {
					int amount = subGoods1.getAmount() - subGoods.getAmount();
					goods.setGoods_num(goods.getGoods_num() - amount);
				} else if (subGoods1.getAmount() < subGoods.getAmount()) {
					int amount = subGoods.getAmount() - subGoods1.getAmount();
					goods.setGoods_num(goods.getGoods_num() + amount);
				}
			} else if(subGoods1.getOperateType().equals("归还") && subGoods.getOperateType().equals("领用")) {
				if (subGoods1.getAmount() == subGoods.getAmount()) {
					int amount = subGoods.getAmount() + subGoods1.getAmount();
					goods.setGoods_num(goods.getGoods_num() - amount);
				} else if (subGoods1.getAmount() > subGoods.getAmount()) {
					int amount = subGoods.getAmount() + subGoods1.getAmount();
					goods.setGoods_num(goods.getGoods_num() - amount);
				} else if (subGoods1.getAmount() < subGoods.getAmount()) {
					int amount = subGoods.getAmount() + subGoods1.getAmount();
					goods.setGoods_num(goods.getGoods_num() - amount);
				}

			}else if (subGoods1.getOperateType().equals("归还") && subGoods.getOperateType().equals("报废")) {
				if (subGoods1.getAmount() == subGoods.getAmount()) {
					int amount = subGoods.getAmount() + subGoods1.getAmount();
					goods.setGoods_num(goods.getGoods_num() - amount);
				} else if (subGoods1.getAmount() > subGoods.getAmount()) {
					int amount = subGoods.getAmount() + subGoods1.getAmount();
					goods.setGoods_num(goods.getGoods_num() - amount);
				} else if (subGoods1.getAmount() < subGoods.getAmount()) {
					int amount = subGoods.getAmount() + subGoods1.getAmount();
					goods.setGoods_num(goods.getGoods_num() - amount);
				}
			}
			long gos = goodsMapper.updateGoods(goods);
			System.out.println("修改成功" + gos);

		// TODO Auto-generated method stub
		return subGoodsMapper.updateSubgoods(subGoods);
	}

	@Override
	public SubGoods getSubgoods(int id) {
		// TODO Auto-generated method stub
		return subGoodsMapper.getSubgoods(id);
	}

	@Override
	public void deleteSubgoodsById(int[] ids) {
		// TODO Auto-generated method stub
		for(int id:ids) {
			subGoodsMapper.deleteSubgoodsById(id);
		}
	}

}
